/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject;

import javax.inject.Inject;

import org.apache.commons.io.output.ByteArrayOutputStream;

import ru.irenproject.UiProtos.QuestionViewerShape;
import ru.irenproject.it.ItOutput;
import ru.irenproject.it.ItProducer;
import ru.irenproject.it.ItWriter;
import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Stash;

import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import com.google.protobuf.Message;

public final class QuestionViewer extends AbstractThing implements Visual {
  @Stash private UiProtos.QuestionViewer.Builder $;
  private final ItOutput fItOutput;

  @Inject QuestionViewer(ItOutput itOutput) {
    fItOutput = itOutput;
  }

  @Override protected void initialize() {
    $.setVersion(1);
  }

  private QuestionItem questionItem() {
    return $.hasQuestionItem() ? get($.getQuestionItem(), QuestionItem.class) : null;
  }

  public void setQuestionItem(QuestionItem item) {
    if (item != questionItem()) {
      if (item == null) {
        $.clearQuestionItem();
      } else {
        $.setQuestionItem(item.id());
      }

      incrementVersion();
    }
  }

  @Override public Message render() {
    QuestionViewerShape.Builder b = QuestionViewerShape.newBuilder()
        .setVersion($.getVersion());
    final QuestionItem item = questionItem();
    if (item != null) {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      fItOutput.writeFragment(new ItProducer() {
        @Override public void produce(ItWriter out) {
          out.writeQuestionItem(item);

          if (sectionPathSupplier() != null) {
            for (Section section : Lists.reverse(sectionPathSupplier().sectionPath())) {
              out.writeModifierList(section.modifierList());
            }
          }

          if (profileSupplier() != null) {
            out.writeModifierList(profileSupplier().profile().modifierList());
          }
        }
      }, buffer);
      b.setItStream(ByteString.copyFrom(buffer.toByteArray()));
    }

    return b.build();
  }

  @Action(markAsModified = false)
  public void doRefresh() {
    incrementVersion();
  }

  private void incrementVersion() {
    $.setVersion($.getVersion() + 1);
  }

  private ProfileSupplier profileSupplier() {
    return $.hasProfileSupplier() ? get($.getProfileSupplier(), ProfileSupplier.class) : null;
  }

  public void setProfileSupplier(ProfileSupplier supplier) {
    if (supplier == null) {
      $.clearProfileSupplier();
    } else {
      $.setProfileSupplier(supplier.id());
    }
  }

  private SectionPathSupplier sectionPathSupplier() {
    return $.hasSectionPathSupplier() ?
        get($.getSectionPathSupplier(), SectionPathSupplier.class) : null;
  }

  public void setSectionPathSupplier(SectionPathSupplier supplier) {
    if (supplier == null) {
      $.clearSectionPathSupplier();
    } else {
      $.setSectionPathSupplier(supplier.id());
    }
  }
}
